from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from numpy import piSchrödinger had many pets it seems. This worm exhibits quantum behavior that’s fun to examine.
I found this exercise in a book named “Quantum Computing for Quantum Curious”

Create a worm in a superposition state of alive and dead. Let q[0] correspond to the bit on the far right.
def create_circuit(n, qreg_q, creg_c):
circuit = QuantumCircuit(qreg_q, creg_c)
for _ in range(n):
circuit.x(qreg_q[_])
circuit.h(qreg_q[0])
return circuit
n = 5
qreg_q = QuantumRegister(n, 'q')
creg_c = ClassicalRegister(n, 'c')
circuit = create_circuit(n, qreg_q, creg_c )The first qubit is in superposition as seen in the bloch spehere
from qiskit.quantum_info import Statevector
state = Statevector(circuit)
plot_bloch_multivector(state)
circuit.measure(qreg_q, creg_c)
circuit.draw('mpl')
from qiskit import Aer, execute
simulator = Aer.get_backend('qasm_simulator')
counts = execute(circuit, backend=simulator, shots=1024).result().get_counts(circuit)counts{'11110': 501, '11111': 523}
from qiskit.visualization import plot_histogram, plot_bloch_multivector
plot_histogram(counts)
Modify the circuit so that the worm is first put in a superposition state and then brought to life
circuit = create_circuit(n, qreg_q, creg_c )
circuit.h(qreg_q[0])<qiskit.circuit.instructionset.InstructionSet at 0x7f7d2c118cd0>
state = Statevector(circuit)
plot_bloch_multivector(state)
circuit.measure(qreg_q, creg_c)
circuit.draw('mpl')
counts = execute(circuit, backend=simulator, shots=1024).result().get_counts(circuit)
plot_histogram(counts)
Modify the circuit so that the worm in a superposition state becomes definitely dead
circuit = create_circuit(n, qreg_q, creg_c )
circuit.h(qreg_q[0])
circuit.x(qreg_q[4])<qiskit.circuit.instructionset.InstructionSet at 0x7f7d0d627940>
state = Statevector(circuit)
plot_bloch_multivector(state)
circuit.measure(qreg_q, creg_c)
circuit.draw('mpl')
counts = execute(circuit, backend=simulator, shots=1024).result().get_counts(circuit)
plot_histogram(counts)

The worm is next to a hungry bird, such that the worm is either alive or chomped to pieces as figure above
Create a circuit that produces a worm in a superposition state of alive and very dead.
circuit = create_circuit(n, qreg_q, creg_c )
circuit.x(qreg_q[0])
circuit.x(qreg_q[2])
circuit.cx(0, 2)<qiskit.circuit.instructionset.InstructionSet at 0x7f7d0f1b7580>
state = Statevector(circuit)
plot_bloch_multivector(state)
circuit.measure(qreg_q, creg_c)
circuit.draw('mpl')
counts = execute(circuit, backend=simulator, shots=1024).result().get_counts(circuit)
plot_histogram(counts)
# Do not run, just a test to see where x H x will produce the same result as x x Hfrom qiskit.visualization import array_to_latexcrk = QuantumCircuit(2)
crk.x(0)
crk.x(0)
crk.h(0)<qiskit.circuit.instructionset.InstructionSet at 0x7f7d0fb73970>
usim = Aer.get_backend('aer_simulator')
crk.save_unitary()
unitary = usim.run(crk).result().get_unitary()array_to_latex(unitary)$$
\[\begin{bmatrix} \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} & 0 & 0 \\ \frac{\sqrt{2}}{2} & - \frac{\sqrt{2}}{2} & 0 & 0 \\ 0 & 0 & \frac{\sqrt{2}}{2} & \frac{\sqrt{2}}{2} \\ 0 & 0 & \frac{\sqrt{2}}{2} & - \frac{\sqrt{2}}{2} \\ \end{bmatrix}\]$$
# and it does# test endsModify the circuit so that the worm in a superposition state becomes either definitely dead or definitely alive
circuit = create_circuit(n, qreg_q, creg_c )
circuit.x(qreg_q[0])
circuit.x(qreg_q[2])
circuit.cx(0, 2)
circuit.cx(0, 2)
circuit.h(qreg_q[0])<qiskit.circuit.instructionset.InstructionSet at 0x7f7d2aef6490>
state = Statevector(circuit)
plot_bloch_multivector(state)
circuit.measure(qreg_q, creg_c)
circuit.draw('mpl')
counts = execute(circuit, backend=simulator, shots=1024).result().get_counts(circuit)
plot_histogram(counts)